import { h, Component } from 'preact';
import { Router } from 'preact-router';
import './../styles/global.common';
import history from '~/core/history';
import Home from '~/containers/Home';
import Ecard from '~/containers/Ecard';
import PostCard from '~/containers/PostCard';
import {wechatInfo} from '~/config';
import wechat, {share} from '~/utils/wechat';
import Request from '~/core/request';
import Address from '~/components/Address';

const url = window.location.href.split('#');

export default class App extends Component {

	constructor(props) {
		super(props);
	}
	componentDidMount() {
		const {appId, title, link, imgUrl, desc} = wechatInfo;
		Request.post(
			'common/auth/JsSdkAuth.do',
			// 'http://wx-test.by-health.com/common/auth/JsSdkAuth.do',
			{
				appId: 'wxb425b33623e260d4',
				url: url[0]
			}).then((res) => {
				console.log(res);
				const {appId, timestamp, nonceStr, signature} = res;
				wechat({
					debug: false,
					appId,
					timestamp,
					nonceStr,
					signature,
					jsApiList:['startRecord', 'stopRecord', 'playVoice', 'translateVoice', 'scanQRCode', 'showAllNonBaseMenuItem']
				}).then(() => {
					console.log(window.wx);
					share(
						{
							title,
							link,
							imgUrl,
							desc
						},
						()=>{console.log('success');}
					);
				});
			}).catch((error) => {
				throw new Error(JSON.stringify(error));
			});
	}
	render() {
		// console.log(this.context);
		return (
			<div id="app"  >
				<Router history={history}>
					<Home path="/" />
					<Ecard path="/ecard" />
					<PostCard path="/postCard"  />
					<Address path="/address" />
				</Router>
			</div>
		);
	}
}
